﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class ConsumeTime
{
    static Dictionary<string, System.Diagnostics.Stopwatch> stopWatchMap = new Dictionary<string, System.Diagnostics.Stopwatch>();
    static bool StartStopWatch(string name)
    {
        if (stopWatchMap.ContainsKey(name))
        {
            return false;
        }

        System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
        sw.Start();

        stopWatchMap.Add(name, sw);
        return true;
    }

    static float StopStopWatch(string name)
    {
        System.Diagnostics.Stopwatch sw = null;
        if (!stopWatchMap.TryGetValue(name, out sw))
        {
            return 0;
        }

        stopWatchMap.Remove(name);
        sw.Stop();
        return sw.ElapsedMilliseconds / 1000f;
    }

    public static void Log(string name, System.Action fun)
    {
        if (!StartStopWatch(name))
        {
            return;
        }

        fun();
        float time = StopStopWatch(name);
        Debug.Log("consume time: " + name + "   " + time);
    }
}
